package com.wanshi.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wanshi.member.base.Page;
import com.wanshi.member.base.Result;
import com.wanshi.member.entity.Goods;
import com.wanshi.member.entity.Member;
import com.wanshi.member.entity.Supplier;
import com.wanshi.member.mapper.SupplierMapper;
import com.wanshi.member.req.MemberREQ;
import com.wanshi.member.req.SupplierREQ;
import com.wanshi.member.service.IGoodsService;
import com.wanshi.member.service.ISupplierService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 供应商信息表 服务实现类
 * </p>
 *
 * @author 顽石教育-www.wanshi.com
 * @since 2021-05-08
 */
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier> implements ISupplierService {


    @Override
    public Result search(long page, long size, SupplierREQ req) {
        QueryWrapper<Supplier> query = new QueryWrapper<>();
        if(req != null) {
            if(StringUtils.isNotBlank(req.getName())) {
                query.like("name", req.getName());
            }
            if(StringUtils.isNotBlank(req.getLinkman())) {
                query.like("linkman", req.getLinkman());
            }
            if(StringUtils.isNotBlank(req.getMobile())) {
                query.like("mobile", req.getMobile());
            }
        }

        query.orderByDesc("id");

        // 分页查询供应商
        IPage<Supplier> data = baseMapper
                .selectPage(new Page<Supplier>(page, size), query);
        return Result.ok(data);
    }


    @Override
    public Result update(int id, Supplier supplier) {
        if(supplier.getId() == null) {
            supplier.setId(id);
        }
        // 更新操作
        int i = baseMapper.updateById(supplier);

        if(i < 1) {
            return Result.error("修改供应商信息失败");
        }
        return Result.ok();
    }
}
